/* @(#)GSOperatorScanQueryAction.java    
*
* Project: BUS
*
* Modify Information:
* =============================================================================
*   Author       Date       Description
*   ------------ ---------- ---------------------------------------------------
*   Simonyi Peng 2006-2-12    建立
*
* Copyright Notice:
* =============================================================================
*    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
*        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
*        Haidian District, Beijing ,100081, China All rights reserved.
*
*    This software is the confidential and proprietary information of
*        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
*        You shall not disclose such Confidential Information and shall use it
*        only in accordance with the terms of the license agreement you entered
*        into with HiTRUST.
*
* Warning:
* =============================================================================
* 
*/
package com.hitrust.gs.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderManufactureVO;
import com.hitrust.gs.vo.GSOrderVO;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.business.GSBusOrderProcesser;
import com.hitrust.gs.business.GSBusOrderProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.form.GSOrderSaveForm;
import java.lang.reflect.Method;

/**
 * @author simonyi
 *
 * 订单保存
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSBusOrderSaveAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
	    
        GSOperatorBean bean = (GSOperatorBean) aForm.getSession().getAttribute(
                GSBaseConstants.LOGIN);

	    GSOrderSaveForm tForm = (GSOrderSaveForm)aForm;
	    
	    GSOrderVO ordervo = new GSOrderVO();
	    
	    
	    //ordervo.orderid = (String)request.getParameter("orderid");
	    ordervo.category = tForm.getCategory();
	    ordervo.notes = tForm.getNotes(); 
	    ordervo.balanceMode = "2".equals(tForm.getBalancemode())?tForm.getBalancemode_():tForm.getBalancemode();
	    ordervo.deliverMode = "2".equals(tForm.getDelivermode())?tForm.getDelivermode_():tForm.getDelivermode();
	    ordervo.deliverAddr = tForm.getDeliveraddr();
	    ordervo.orderDeptId = tForm.getDeptid();
	    ordervo.isReviewNeeded = tForm.getIsreviewneeded();
	    ordervo.linkMan = tForm.getLinkman_()==null?"":tForm.getLinkman_();
	    ordervo.linkTel = tForm.getLinktel_()==null?"":tForm.getLinktel_();
	    
	    ordervo.operid = bean.getGs_oper_id();
	    ordervo.orgid = bean.getGs_oper_org_id();
	    ordervo.balanceCategory = tForm.getBalancecategory();
	    
	    ArrayList ary = new ArrayList();
	    
	    String[] concentricityId = tForm.getConcentricityid();
	    String[] manufactureId = tForm.getManufactureid();
	    String[] orderCount = tForm.getOrdercount();
	    String[] deliverDate = tForm.getDeliverdate();
	    String[] deliverTime = tForm.getDelivertime();
	    String[] manufactureUnitPrice = tForm.getManufactureunitprice();
	    String[] mode=tForm.getMode();
	    try{
		    for(int i=0;i<10;i++){
		        GSOrderManufactureVO vo = new GSOrderManufactureVO();
		        
		        if(concentricityId[i] == null || concentricityId[i].length() == 0)
		            continue;
		        vo.concentricityId = concentricityId[i];
		        vo.manufactureid = manufactureId[i];
		        vo.ordercount = orderCount[i];
		        vo.deliverdate = deliverDate[i];
		        vo.delivertime = deliverTime[i];
		        vo.manufactureUnitPrice = Double.parseDouble("".equals(manufactureUnitPrice[i])?"0":manufactureUnitPrice[i]);
		        vo.balanceCategory = ordervo.balanceCategory;
		        vo.mode=mode[i];
		        ary.add(vo);
		    }
	        
	    }catch(Exception e){}
	    
	    GSBusOrderProcessor processor = new GSBusOrderProcessor();
	    GSBusOrderProcesser processer = new GSBusOrderProcesser();
	    try{
	        String orderId = processor.saveOrder(bean,ordervo,ary);//"010000540";
	        
	        String saveOption = aForm.getRequest().getParameter("saveoption");
	        if("SA".equals(saveOption)){
	            aForm.getRequest().setAttribute("SUCCESSMSG","订单编号 "+orderId+" 已成功提交");
	            return "new";//允许录入新订单
	        }
	        if("S".equals(saveOption)){
		        ArrayList arylist = processer.getOrderDetail(orderId);
		        GSOrderBean tBean = processer.selectOrderByCode(orderId);
		        
		        aForm.getRequest().setAttribute("bean",tBean);
		        aForm.getRequest().setAttribute("arylist",arylist);
		        ArrayList bcList = GSBaseManager.getAllBalanceCategory();
		        aForm.getRequest().setAttribute("balancecategory",bcList);
		        aForm.getRequest().setAttribute("SUCCESSMSG","订单编号 "+orderId+" 已成功提交");
		        return GSBaseConstants.FORWARD_SUCCESS;
	        }
	        return GSBaseConstants.FORWARD_FAILURE;
	    }
	    catch(GSException e){
	        e.printStackTrace();
			GSLogWriter.error(this, e.getMessage());
			aForm.getRequest().setAttribute("action", "A150101.do");
			throw e;	        
	    }
	    
	}
}
